package com.iot.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iot.commonutils.utils.Result;
import com.iot.user.entity.User;
import com.iot.user.entity.UserRole;
import com.iot.user.mapper.UserMapper;
import com.iot.user.mapper.UserRoleMapper;
import com.iot.user.service.UserListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


import java.util.Date;
import java.util.List;

@Service
public class UserListServiceImpl extends ServiceImpl<UserMapper, User> implements UserListService {
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 展示用户列表
     *
     * @return
     */
    @Override
    public Result showUserList(User vo) {
        Page page = new Page(vo.getPage(), vo.getLimit());
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(vo.getName())){
            wrapper.like("name",vo.getName());
        }

        IPage<User> userIPage = userMapper.selectPage(page,  wrapper);
        return Result.success("展示列表成功", userIPage);
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @Override
    public Result addUserList(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        QueryWrapper<User> name = queryWrapper.eq("name", user.getName());
        List<User> listUser = list(name);
        if (listUser != null && listUser.size() == 1) {

            return Result.error("用户名已经存在,请重新输入");
        }
        user.setCreatedTime(new Date());
        user.setLoginTime(new Date());
        user.setStatus(9);
        user.setSalt(user.getName());
        String md5DigestAsHex = DigestUtils.md5DigestAsHex((user.getPassword() + user.getSalt()).getBytes());
        user.setPassword(md5DigestAsHex);
        //保存用户
        save(user);
        //给注册用户加权限
        QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
        QueryWrapper<User> name1 = queryWrapper1.eq("name", user.getName());
        List<User> listUser1 = list(name1);
        User user1 = listUser1.get(0);
        UserRole userRole = new UserRole();
        userRole.setUserId(user1.getId());
        userRole.setRoleId("2");
        userRoleMapper.insert(userRole);
        return Result.success("用户插入成功!");
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteUserList(String id) {

        int result = userMapper.deleteById(id);
        if (result == 1) {
            return Result.success("用户删除成功!");
        }
        return Result.error("用户删除失败,请重试!");
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @Override
    public Result changeUserList(User user) {
        if (StringUtils.isEmpty(user.getNickname()) && StringUtils.isEmpty(user.getImage())
                && StringUtils.isEmpty(user.getName()) && StringUtils.isEmpty(user.getPassword())) {
            return Result.error("账号,密码,昵称,头像不能为空!");
        }
        //把用户的密码加密
        String md5DigestAsHex = DigestUtils.md5DigestAsHex((user.getPassword() + user.getSalt()).getBytes());
        user.setPassword(md5DigestAsHex);
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", user.getId());
        update(user, wrapper);
        return Result.success("用户信息修改成功!");
    }

    /**
     * 模糊查询用户信息
     *
     * @param name
     * @return
     */
    @Override
    public Result fuzzyUserList(String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.likeRight("name", name);
        List<User> users = userMapper.selectList(wrapper);
        return Result.success("查询成功!!!", users);
    }

    /**
     * 给用户分配角色
     *
     * @param roleId
     * @return
     */
    @Override
    public Result roleUserList(String userId, String roleId) {
        if (userId != null && roleId != null) {
            QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
            QueryWrapper<UserRole> userId1 = wrapper.eq("user_id", userId);
            UserRole userRole = new UserRole();
            userRole.setRoleId(roleId);
            userRoleMapper.update(userRole, userId1);
            return Result.success("用户权限修改成功!");
        } else {
            return Result.error("用户ID或者权限没设置,修改权限失败!");
        }
    }


}
